using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;

// TODO: replace these with the processor input and output types.
using TInput = System.String;
using TOutput = System.String;
using System.Text;

namespace TestCraftPipeline
{
    /// <summary>
    /// This class will be instantiated by the XNA Framework Content Pipeline
    /// to apply custom processing to content data, converting an object of
    /// type TInput to TOutput. The input and output types may be the same if
    /// the processor wishes to alter data without changing its type.
    ///
    /// This should be part of a Content Pipeline Extension Library project.
    ///
    /// TODO: change the ContentProcessor attribute to specify the correct
    /// display name for this processor.
    /// </summary>
    [ContentProcessor(DisplayName = "TestCraftPipeline.ContentProcessor1")]
    public class ContentProcessor1 : ModelProcessor
    {
        private List<VertexPositionNormalTexture> stuff;
        private List<int> ind;
        public override ModelContent Process(NodeContent input, ContentProcessorContext context)
        {
            ModelContent content = base.Process(input, context);
            stuff = new List<VertexPositionNormalTexture>();
            ind = new List<int>();
            System.Diagnostics.Debugger.Launch();
            foo(input);

            StringBuilder builder = new StringBuilder();
            builder.Append("VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[] {\n");
            builder.Append(string.Join(",", stuff.Select(x => string.Format("new VertexPositionNormalTexture(new Vector3({0}, {1}, {2}), new Vector3({3}, {4}, {5}), new Vector2({6}, {7}))", x.Position.X, x.Position.Y, x.Position.Z, x.Normal.X, x.Normal.Y, x.Normal.Z, x.TextureCoordinate.X, x.TextureCoordinate.Y))));
            builder.Append("\n};\n\n");

            builder.Append("int[] indices = new int[] {\n");
            builder.Append(string.Join(",", ind));
            builder.Append("\n};");

            string code = builder.ToString();
              
            return content;
        }

        private void foo(NodeContent node)
        {
            MeshContent mesh = node as MeshContent;
            if (mesh != null)
            {

                foreach (GeometryContent geometry in mesh.Geometry)
                {
                    for (int i = 0; i < geometry.Vertices.VertexCount; i++)
                    {
                        stuff.Add(new VertexPositionNormalTexture(
                            geometry.Vertices.Positions[i],
                            (Vector3)geometry.Vertices.Channels[VertexChannelNames.Normal(0)][i],
                            (Vector2)geometry.Vertices.Channels[VertexChannelNames.TextureCoordinate(0)][i]));
                    }

                    for (int i = 0; i < geometry.Indices.Count; i++)
                    {
                        ind.Add(geometry.Indices[i]);
                    }
                    //for (int i = 0; i < geometry.Indices.Count; i++)
                    //{
                    //    stuff.Add(new VertexPositionNormalTexture(
                    //        geometry.Vertices.Positions[geometry.Indices[i]],
                    //        (Vector3)geometry.Vertices.Channels[VertexChannelNames.Normal(0)][geometry.Indices[i]],
                    //        (Vector2)geometry.Vertices.Channels[VertexChannelNames.TextureCoordinate(0)][geometry.Indices[i]]));
                    //}
                }
            }
            else
            {
            }

            foreach (NodeContent child in node.Children)
            {
                foo(child);
            }
        }
    }
}